package com.lyh.poi.controller.easyPoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lyh
 * @date 2022-04-10
 */
@Api(value = "excel模板导出",tags = "excel模板导出")
@RestController
@RequestMapping("/easyPoi/template")
public class ExcelTemplateOutController {

    /**
     * 月报表 导出excel表
     */
    @GetMapping(value = "/out/report", produces = "application/octet-stream")
    @ApiOperation(value = "月报表 导出excel表")
    public void excelout(HttpServletResponse response) {
        Map<String, Object> total = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i + "");
            map.put("enTimea", i+"月");
            map.put("enTimeb", "本年截止0"+i+"月累计");

            //数据中的第一列
            map.put("enBenyue", "本月" + i);
            map.put("enShangyue", "上月" + i);
            map.put("enTongbi", "内容"+i);
            map.put("leijiyongliang", "累计用量" + i);
            map.put("tongbizengzhang", "同比增长" + i);
            //数据中的第二列
            map.put("enBenyue1", "本月" + i);
            map.put("enShangyue1", "上月" + i);
            map.put("enTongbi1", "内容"+i);
            map.put("leijiyongliang1", "累计用量" + i);
            map.put("tongbizengzhang1", "同比增长" + i);

            mapList.add(map);
        }
        total.put("list", mapList);

        String templateName = "月报表.xlsx";
        TemplateExportParams params = new TemplateExportParams("template/" + templateName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, total);
        ServletOutputStream out = null;
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(templateName, "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 货品信息 导出excel表
     */
    @GetMapping(value = "/out", produces = "application/octet-stream")
    @ApiOperation(value = "货品信息 导出excel表")
    public void excel(HttpServletResponse response) {
        Map<String, Object> total = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i + "");
            map.put("bin", "001 1000千克");
            map.put("name", "商品" + i);
            map.put("code", "goods" + i);
            map.put("proDate", "2021-02-12");
            map.put("recvDate", "2022-04-07");

            mapList.add(map);
        }
        total.put("list", mapList);

        String templateName = "货品信息.xlsx";
        TemplateExportParams params = new TemplateExportParams("template/" + templateName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, total);
        ServletOutputStream out = null;
        try {
            response.setHeader("content-type", "application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(templateName, "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 测试导出excel表，指定导出位置
     */
    @GetMapping(value = "/out/test", produces = "application/octet-stream")
    @ApiOperation(value = "测试导出excel表，指定导出位置")
    public String excelTest() {
        Map<String, Object> total = new HashMap<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i + "");
            map.put("bin", "001 1000千克");
            map.put("name", "商品" + i);
            map.put("code", "goods" + i);
            map.put("proDate", "2021-02-12");
            map.put("recvDate", "2022-04-07");

            mapList.add(map);
        }
        total.put("list", mapList);

        String templateName = "货品信息.xlsx";
        TemplateExportParams params = new TemplateExportParams("template/" + templateName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, total);

        try {
            //存储位置
            File savefile = new File("D:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            //创建文件'库别:'
            FileOutputStream fileOut = new FileOutputStream("D:/excel/"+templateName);
            workbook.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
            return "导出失败"+e;
        }
        return "导出成功，导出文件位置："+"D:/excel/"+templateName;
    }


}
